import { $get, $put } from '@js/authRequest'
import { awaitWrap } from '@js/tool'

// 获取送达详情数据
export async function getDetailRequest (id) {
  const res = await $get(`/api/xapc/deliver/${id}`)
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return res.data
}

// 更新送达数据
export async function updateModalData (id, data) {
  let detail = await getDetailRequest(id)
  detail = { ...detail, ...data }
  delete detail.id
  const res = await $put(`/api/xapc/deliver/${id}`, detail)
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return res.data
}

// 更新送达状态
export async function updateModalStatus (id, status) {
  const [e] = await awaitWrap(updateModalData(id, { status }))
  if (e) {
    throw new Error(e.message)
  }
}

// 获取邮单数据
export async function getEmsDetailRequest (id, caseid) {
  const res = await $get(`/api/vendor/file/findByBizid/${id}`, {
    tempcode: 'ems',
    caseid
  })
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return res.data
}

// 发送电子送达请求
export async function sendElectronicRequest (deliverid) {
  const res = await $put('/api/xapc/deliver/sendDigitalDeliver', {
    deliverid
  })
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  // await updateModalData(deliverid, { send_datetime: Math.floor(Date.now() / 1000) })
  return res.data
}
